package ru.ognick.coachsystem.editor;


import java.util.ArrayList;

import ru.ognick.coachsystem.common.Exercise;
import ru.ognick.coachsystem.common.Training;
import ru.ognick.coachsystem.common.base.BaseExerciseAdapter;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class EditExerciseAdapter extends BaseExerciseAdapter<EditExerciseView> {
    public final String TAG = getClass().getSimpleName();	
	
	public void restoreOpen() {
		for (int i : ((EditorActivity) getContext()).openExercises) 
			getItem(i).show();
	}
	
	public void hideAllItem(EditExerciseView selectedView) {
		for (EditExerciseView editExerciseView : exercises) {
			if (selectedView != editExerciseView)
				editExerciseView.hide();
		}
	}
	
	public void holdData() {
		for (EditExerciseView editExerciseView : exercises) 
			editExerciseView.holdData();
	}
	
	public EditExerciseAdapter(Context context, 
							   int textViewResourceId, 
							   ArrayList<EditExerciseView> exercises) {
		super(context, textViewResourceId, exercises);
	}
	
	public EditExerciseView add(Exercise exercise){
        final EditExerciseView editExerciseView = new EditExerciseView(getContext(), exercise); 
		exercises.add(editExerciseView);
		editExerciseView.getTitleBox().setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				EditExerciseAdapter a = EditExerciseAdapter.this; 
				a.hideAllItem(editExerciseView);
				ArrayList<Integer> opens = ((EditorActivity) a.getContext()).openExercises;
				Integer index = a.getPosition(editExerciseView);
				if (editExerciseView.autoVisibility()) 
					opens.add(index);
				else 
					opens.remove(index);
				EditExerciseAdapter.this.notifyDataSetChanged();
			}
		});
        notifyDataSetChanged();
        return editExerciseView;
	}
}
